package com.fivefu.core.data.services.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fivefu.base.common.utils.str.StrUtils;
import com.fivefu.core.data.constant.PlatformEnum;
import com.fivefu.core.data.entity.DbFfskyeyeChannel;
import com.fivefu.core.data.entity.DbFfskyeyeDevice;
import com.fivefu.core.data.entity.DbFfskyeyePlatform;
import com.fivefu.core.data.entity.DbFfskyeyeRegion;
import com.fivefu.core.data.mapper.DbFfskyeyeChannelMapper;
import com.fivefu.core.data.properties.StreamServiceMapProperties;
import com.fivefu.core.data.properties.StreamServiceProperties;
import com.fivefu.core.data.services.DbFfskyeyeChannelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fivefu.core.data.services.DbFfskyeyeDeviceService;
import com.fivefu.core.data.services.DbFfskyeyePlatformService;
import com.fivefu.core.data.services.DbFfskyeyeRegionService;
import com.fivefu.core.hik.entity.DbHikCamera;
import com.fivefu.core.hik.services.DbHikCameraService;
import com.fivefu.core.hik.services.VideoService;
import com.fivefu.core.hillstone.entity.DbHillstoneChannel;
import com.fivefu.core.hillstone.entity.DbHillstoneChannelAbility;
import com.fivefu.core.hillstone.services.DbHillstoneChannelAbilityService;
import com.fivefu.core.hillstone.services.DbHillstoneChannelService;
import com.fivefu.core.hillstone.services.HillstoneService;
import com.fivefu.module.dictionary.entity.DbSysDict;
import com.fivefu.module.dictionary.service.DbSysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 通道 服务实现类
 * </p>
 *
 * @author 
 * @since 2022-09-02 13:38:22
 */
@Service
public class DbFfskyeyeChannelServiceImpl extends ServiceImpl<DbFfskyeyeChannelMapper, DbFfskyeyeChannel> implements DbFfskyeyeChannelService {

    @Autowired
    private VideoService videoService;

    @Autowired
    private DbHillstoneChannelService dbHillstoneChannelService;
    @Autowired
    private HillstoneService hillstoneService;
    @Autowired
    private DbFfskyeyeDeviceService dbFfskyeyeDeviceService;
    @Autowired
    private DbSysDictService dbSysDictService;
    @Autowired
    private DbHikCameraService dbHikCameraService;
    @Autowired
    private DbHillstoneChannelAbilityService dbHillstoneChannelAbilityService;
    @Autowired
    private DbFfskyeyeRegionService dbFfskyeyeRegionService;
    @Autowired
    private DbFfskyeyePlatformService dbFfskyeyePlatformService;
    @Autowired
    private StreamServiceMapProperties streamServiceMapProperties;

    /**
     * 通过通道唯一标识查询播放地址
     * @param channelCode   通道唯一标识
     * @param protocal      取流协议(共同协议：rtsp,rtmp,hls,海康额外有：hik,ws,大华额外有：FLV_HTTP)
     * @param subType       码流类型(0:主码流,1:辅流1,2:辅流2。默认为0主码流)只有大华有
     * @return
     */
    @Override
    public String queryUrlByChannelCode(String channelCode,String protocal,Integer subType) {
        //根据通道唯一标识判断是海康还是大华
        DbFfskyeyeChannel channel = queryByChannelCode(channelCode);
        if (PlatformEnum.HIKVERSION.getPlatformCode().equals(channel.getPlatformCode())) {
            //判断protocal海康是否包含该类型
            if (StrUtils.isNotNull(protocal)) {
                DbSysDict dict = dbSysDictService.queryDictByValue("SPT",protocal);
                if (null == dict && !protocal.equals("FLV_HTTP")) {
                    return null;
                }
            } else {
                protocal = null;
            }
            //根据海康平台通道唯一标识查询播放地址
            String cameraPreviewURLs = videoService.getCameraPreviewURLs(channel.getPlatformChannelCode(), protocal);
            return cameraPreviewURLs;
        } else if (PlatformEnum.HILLSTONE.getPlatformCode().equals(channel.getPlatformCode())) {
            //登录大华
            try {
                hillstoneService.login();
            } catch (IOException e) {
                e.printStackTrace();
            }
            //判断protocal大华是否包含该类型
            if (StrUtils.isNotNull(protocal)) {
                DbSysDict dict = dbSysDictService.queryDictByValue("SPT",protocal);
                if (null == dict && !protocal.equals("hik") && !protocal.equals("ws")) {
                    return null;
                }
                protocal = protocal.toUpperCase();
            } else {
                protocal = null;
            }
            //判断subType大华是否包含此码流
            if (null != subType) {
                DbSysDict dict = dbSysDictService.queryDictByValue("SA",subType.toString());
                if (null == dict) {
                    return null;
                }
            } else {
                subType = null;
            }
            //根据大华平台通道唯一标识查询播放地址
            JSONObject resultJSON = dbHillstoneChannelService.getRealTimeMonitoringURI(channel.getPlatformChannelCode(),subType , protocal);
            //销毁会话
            hillstoneService.destoryToken();
            if (null == resultJSON) {
                return null;
            }
            String url = resultJSON.toString();
            return url;
        } else {
            String platform = channel.getPlatformCode();
            if (streamServiceMapProperties.getPreviewUrlService() != null && streamServiceMapProperties.getPreviewUrlService().containsKey(platform)){
                StreamServiceProperties streamService = streamServiceMapProperties.getPreviewUrlService().get(platform);
                Map<String, String> paramMap = new HashMap<>();
                paramMap.put("channelCode", channelCode);
                paramMap.put("protocal", protocal);
                if (subType != null) {
                    paramMap.put("subType", subType.toString());
                }
                String url =  streamServiceMapProperties.queryStreamService(streamService, paramMap);
                if (StrUtils.isNotNull(url)){
                    return "{\"url\":\"" + url + "\"}";
                }
            }
        }
        return null;
    }

    /**
     *
     * @param formatList
     * @param platFormCode  平台标记
     */
    private void insertCollectChannel(List<DbFfskyeyeChannel> formatList, String platFormCode) {
        LocalDateTime time = LocalDateTime.now();
        for (DbFfskyeyeChannel channel : formatList) {
            channel.setCreatedBy("sys");
            channel.setCreatedTime(time);
            channel.setUpdatedBy("sys");
            channel.setUpdatedTime(time);
            channel.setIsDelete(0);
            channel.setPlatformCode(platFormCode);
            //使用uuid生成通道唯一标识
            String s = UUID.randomUUID().toString();
            channel.setChannelCode(s);
            DbFfskyeyeDevice dbFfskyeyeDevice = dbFfskyeyeDeviceService.queryDevice(channel.getPlatformDeviceCode(),platFormCode);
            channel.setDeviceId(dbFfskyeyeDevice.getId());
        }
        saveBatch(formatList);
    }

    /**
     * 删除通道信息
     * @param channelList2
     */
    private void deleteCollectChannelById(List<DbFfskyeyeChannel> channelList2) {
        for (DbFfskyeyeChannel channel : channelList2) {
            channel.setIsDelete(1);
            channel.setUpdatedBy("sys");
            channel.setUpdatedTime(LocalDateTime.now());
            baseMapper.updateById(channel);
        }
    }

    /**
     * 通过通道唯一标识查询相关数据信息
     * @param channelCode  通道唯一标识
     * @return
     */
    @Override
    public DbFfskyeyeChannel queryByChannelCode(String channelCode) {
        QueryWrapper<DbFfskyeyeChannel> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        wrapper.eq("channel_code",channelCode);
        DbFfskyeyeChannel channel = baseMapper.selectOne(wrapper);
        return channel;
    }

    /**
     * 查询多个通道流地址
     * @param channelCodes  通道唯一标识集（多个用逗号隔开）
     * @param protocal      取流协议(共同协议：rtsp,rtmp,hls,海康额外有：hik,ws,大华额外有：FLV_HTTP)
     * @param subType       码流类型(0:主码流,1:辅流1,2:辅流2。默认为0主码流)只有大华有
     * @return
     */
    @Override
    public Map<String,String> getUrlBychannelCodes(String channelCodes,String protocal,Integer subType) {
        Map<String,String> resultMap = new HashMap<>();
        String[] split = channelCodes.split(",");
        for (String channelCode : split) {
            String result = queryUrlByChannelCode(channelCode, protocal, subType);
            if (StrUtils.isNotNull(result)) {
                String url = JSONObject.parseObject(result).getString("url");
                resultMap.put(channelCode, url);
            }
        }
        return resultMap;
    }


    /**
     * 转换海康返回的数据信息
     * @param cameraList
     * @return
     */
    private List<DbFfskyeyeChannel> formatHik(List<DbHikCamera> cameraList, String platformCode) {
        List<DbFfskyeyeChannel> resultList = new ArrayList<>();
        if (null == cameraList || cameraList.size() <= 0) {
            return resultList;
        }
        cameraList.forEach(camera -> {
            DbFfskyeyeChannel channel = new DbFfskyeyeChannel();
            if (StrUtils.isNotNull(camera.getChannelNo())) {
                channel.setChannelSeq(Integer.valueOf(camera.getChannelNo()));//通道序号
            }
            channel.setPlatformChannelCode(camera.getCameraIndexCode());//平台通道唯一标识
            if (StrUtils.isNotNull(camera.getChannelType())) {
                channel.setChannelType(camera.getChannelType());//通道类型
            }
            if (StrUtils.isNotNull(camera.getCameraName())) {
                channel.setChannelName(camera.getCameraName());//通道名称
            }
            if (StrUtils.isNotNull(camera.getCapability())) {
                channel.setCameraFuncitons(camera.getCapability());//编码通道功能
            }
            if (StrUtils.isNotNull(camera.getCameraType())) {
                channel.setCameraType(camera.getCameraType());//摄像头类型
            }
            if (null != camera.getLongitude()) {
                channel.setLongitude(camera.getLongitude());//x坐标
            }
            if (null != camera.getLatitude()) {
                channel.setLatitude(camera.getLatitude());//y坐标
            }
            if (null != camera.getOnlineStatus()) {
                if (1 == camera.getOnlineStatus()) {
                    channel.setOnline(camera.getOnlineStatus());//是否在线
                } else {
                    channel.setOnline(0);//是否在线
                }
            }
            if (StrUtils.isNotNull(camera.getRegionIndexCode())) {//平台域唯一编号
                channel.setPlatformRegionCode(camera.getRegionIndexCode());
                DbFfskyeyeRegion region = dbFfskyeyeRegionService.queryRegion(camera.getRegionIndexCode(),platformCode);
                if (null != region) {
                    channel.setRegionId(region.getId());
                }
            }
            if (StrUtils.isNotNull(camera.getEncodeDevIndexCode())) {
                channel.setPlatformDeviceCode(camera.getEncodeDevIndexCode());//平台设备唯一标识
                DbFfskyeyeDevice device = dbFfskyeyeDeviceService.queryDevice(channel.getPlatformDeviceCode(),platformCode);
                if (null != device) {
                    channel.setDeviceId(device.getId());
                }
            }
            channel.setPlatformCode(platformCode);
            resultList.add(channel);
        });
        return resultList;
    }

    /**
     * 转换大华返回的数据信息
     * @param channelList
     * @return
     */
    private List<DbFfskyeyeChannel> formatHillstone(List<DbHillstoneChannel> channelList,String platformCode) {
        List<DbFfskyeyeChannel> resultList = new ArrayList<>();
        if (null == channelList || channelList.size() <= 0) {
            return resultList;
        }
        channelList.forEach(hillChannel -> {
            DbFfskyeyeChannel channel = new DbFfskyeyeChannel();
            if (null != hillChannel.getChannelSeq()) {
                channel.setChannelSeq(Integer.valueOf(hillChannel.getChannelSeq()));//通道序号
            }
            if (StrUtils.isNotNull(hillChannel.getChannelCode())) {
                channel.setPlatformChannelCode(hillChannel.getChannelCode());//平台通道唯一标识
                List<DbHillstoneChannelAbility> abilityList = dbHillstoneChannelAbilityService.queryChannelAbility(channel.getPlatformChannelCode());
                if (null != abilityList && abilityList.size() > 0) {
                    List<String> abilities = abilityList.stream().map(k -> k.getChannelAbility()).collect(Collectors.toList());
                    String s = String.join(",",abilities);
                    channel.setCameraFuncitons(s);//编码通道功能
                }
            }
            if (StrUtils.isNotNull(hillChannel.getChannelType())) {
                channel.setChannelType(hillChannel.getChannelType());//通道类型
            }
            if (StrUtils.isNotNull(hillChannel.getName())) {
                channel.setChannelName(hillChannel.getName());//通道名称
            }
            if (null != hillChannel.getCameraType()) {
                String number = "";
                if (1 == hillChannel.getCameraType()) {
                    number += 0;
                } else if (2 == hillChannel.getCameraType()) {
                    number += 4;
                } else if (3 == hillChannel.getCameraType()) {
                    number += 1;
                }
                DbSysDict dict = dbSysDictService.queryDictByValue("CTP", number);
                if (null != dict) {
                    channel.setCameraType(dict.getDictCode());//摄像头类型
                }
            }
            if (StrUtils.isNotNull(hillChannel.getGpsX())) {
                channel.setLongitude(new BigDecimal(hillChannel.getGpsX()));//x坐标
            }
            if (StrUtils.isNotNull(hillChannel.getGpsY())) {
                channel.setLatitude(new BigDecimal(hillChannel.getGpsY()));//y坐标
            }
            if (null != hillChannel.getOnline()) {
                channel.setOnline(hillChannel.getOnline());//是否在线
            }
            if (StrUtils.isNotNull(hillChannel.getOrganizationCode())) {//平台域唯一编号
                channel.setPlatformRegionCode(hillChannel.getOrganizationCode());
                DbFfskyeyeRegion region = dbFfskyeyeRegionService.queryRegion(hillChannel.getOrganizationCode(),platformCode);
                if (null != region) {
                    channel.setRegionId(region.getId());
                }
            }
            if (StrUtils.isNotNull(hillChannel.getDeviceCode())) {
                channel.setPlatformDeviceCode(hillChannel.getDeviceCode());//平台设备唯一标识
                DbFfskyeyeDevice device = dbFfskyeyeDeviceService.queryDevice(channel.getPlatformDeviceCode(),platformCode);
                if (null != device) {
                    channel.setDeviceId(device.getId());
                }
            }
            channel.setPlatformCode(platformCode);
            resultList.add(channel);
        });
        return resultList;
    }

    /**
     * 更新本地通道数据信息
     * @param addedList
     * @param platFormCode  平台标记
     */
    private void updateFormatChannel(List<DbFfskyeyeChannel> addedList,String platFormCode) {
        if (null == addedList || addedList.size() <= 0) {
            return;
        }
        LocalDateTime time = LocalDateTime.now();
        for (DbFfskyeyeChannel channel : addedList) {
            //根据平台通道唯一标识和平台标记查询相关通道id
            DbFfskyeyeChannel oldChannel = queryChannelByPlatformChannelCode(channel.getPlatformChannelCode(),platFormCode);
            channel.setId(oldChannel.getId());
            channel.setUpdatedTime(time);
            channel.setUpdatedBy("sys");
            baseMapper.updateById(channel);
        }
    }

    /**
     * 根据平台通道唯一标识和平台标记查询相关通道信息
     * @param platformChannelCode   平台通道唯一标识
     * @param platFormCode          平台标记
     * @return
     */
    private DbFfskyeyeChannel queryChannelByPlatformChannelCode(String platformChannelCode, String platFormCode) {
        QueryWrapper<DbFfskyeyeChannel> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        wrapper.eq("platform_channel_code",platformChannelCode);
        wrapper.eq("platform_code",platFormCode);
        DbFfskyeyeChannel channel = baseMapper.selectOne(wrapper);
        return channel;
    }

    private void deleteCoolectChannel(List<String> codeList, String platformChannelCode,String userId) {
        QueryWrapper<DbFfskyeyeChannel> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        if (!codeList.isEmpty()) {
            wrapper.notIn("platform_channel_code",codeList);
        }
        wrapper.eq("platform_code",platformChannelCode);

        DbFfskyeyeChannel channel = new DbFfskyeyeChannel();
        channel.setIsDelete(1);
        channel.setUpdatedTime(LocalDateTime.now());
        channel.setUpdatedBy(userId);
        baseMapper.update(channel,wrapper);
    }

    /**
     * 编辑汇总通道信息
     * @param channel
     * @param platFormCode
     */
    private void editorDbFfskyeyeChannel(DbFfskyeyeChannel channel, String platFormCode,String userId) {
        LocalDateTime time = LocalDateTime.now();
        DbFfskyeyeChannel oldChannel = queryChannel(channel.getPlatformChannelCode(),platFormCode);
        channel.setUpdatedBy(userId);
        channel.setUpdatedTime(time);

        if (null != oldChannel) {
            channel.setId(oldChannel.getId());
            baseMapper.updateById(channel);
        } else {
            channel.setCreatedTime(time);
            channel.setCreatedBy(userId);
            channel.setIsDelete(0);
            String s = UUID.randomUUID().toString();
            channel.setChannelCode(s);
            baseMapper.insert(channel);
        }
    }

    /**
     * 根据平台通道唯一编码及平台标记查询
     * @param platformChannelCode   平台通道唯一编码
     * @param platFormCode          平台标记
     * @return
     */
    private DbFfskyeyeChannel queryChannel(String platformChannelCode, String platFormCode) {
        QueryWrapper<DbFfskyeyeChannel> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        wrapper.eq("platform_channel_code",platformChannelCode);
        wrapper.eq("platform_code",platFormCode);
        DbFfskyeyeChannel channel = baseMapper.selectOne(wrapper);
        return channel;
    }

    /**
     * 查询通道列表
     * @param deleteFlag 删除标识
     * @param platFormCode 平台标识
     */
    @Override
    public List<DbFfskyeyeChannel> queryChannelList(Integer deleteFlag, String platFormCode){
        QueryWrapper<DbFfskyeyeChannel> wrapper = new QueryWrapper<DbFfskyeyeChannel>();
        if (deleteFlag != null){
            wrapper.eq("is_delete",deleteFlag);
        }
        if (StrUtils.isNotNull(platFormCode)) {
            wrapper.eq("platform_code", platFormCode);
        }
        List<DbFfskyeyeChannel> channelList = baseMapper.selectList(wrapper);
        return channelList;
    }

    /**
     * 通道信息汇总入库
     * @param platformName   设备平台：海康，大华
     * @param userId 用户编号
     */
    @Override
    public void channelCollect(String platformName,String userId) {
        DbFfskyeyePlatform platform = dbFfskyeyePlatformService.queryPlatformByName(platformName);
        String platformCode = platformName;
        if (platform != null) {
            platformCode = platform.getPlatformCode();
        }
        List<DbFfskyeyeChannel> resultList = new ArrayList<>();
        if (PlatformEnum.HIKVERSION.getPlatformCode().equals(platformCode)) {
            //查询海康通道表数据
            List<DbHikCamera> cameraList = dbHikCameraService.queryAllCamera();
            if (null == cameraList || cameraList.size() <= 0) {
                return;
            }
            //转换海康通道表返回的数据信息
            resultList = formatHik(cameraList, platformCode);
        } else {
            //查询大华的通道表数据
            List<DbHillstoneChannel> channelList = dbHillstoneChannelService.queryAllChannel();
            if (null == channelList || channelList.size() <0) {
                return;
            }
            //转换大华通道表返回的数据信息
            resultList = formatHillstone(channelList, platformCode);
        }
        List<String> codeList = resultList.stream().map(k -> k.getPlatformChannelCode()).collect(Collectors.toList());

        for (DbFfskyeyeChannel channel : resultList) {
            editorDbFfskyeyeChannel(channel,platformCode,userId);
        }
        //删除多余的海康通道信息数据
        deleteCoolectChannel(codeList,platformCode,userId);
    }

    /**
     * 获取视频回放URI
     * @param channelCode    通道唯一标识
     * @param stime          回放开始时间(yyyy-MM-dd HH:mm:ss)
     * @param etime          回放结束时间(yyyy-MM-dd HH:mm:ss)
     * @param scheme         协议类型，共有：rtsp、rtmp、hls三种,海康额外：hik,ws(海康默认hik,大华默认rtsp)
     * @return
     */
    @Override
    public String queryPlaybackURI(String channelCode, String stime, String etime, String scheme) {
        //根据通道唯一标识判断是海康还是大华
        DbFfskyeyeChannel channel = queryByChannelCode(channelCode);
        if (PlatformEnum.HIKVERSION.getPlatformCode().equals(channel.getPlatformCode())) {
            String url = videoService.getCameraBackURL(channel.getPlatformChannelCode(),null,scheme,stime,etime);
            return url;
        } else if (PlatformEnum.HILLSTONE.getPlatformCode().equals(channel.getPlatformCode())) {
            //登录大华
            try {
                hillstoneService.login();
            } catch (IOException e) {
                e.printStackTrace();
            }
            if ("hik".equals(scheme) || "ws".equals(scheme)) {
                return null;
            }
            JSONObject resultJSON = dbHillstoneChannelService.getPlaybackURI(channel.getPlatformChannelCode(),stime,etime,"cloud",scheme.toUpperCase(),null);
            //销毁会话
            hillstoneService.destoryToken();
            if (null == resultJSON) {
                return null;
            }
            String url = resultJSON.toString();
            return url;
        } else {
            String platform = channel.getPlatformCode();
            if (streamServiceMapProperties.getRecordUrlService() != null && streamServiceMapProperties.getRecordUrlService().containsKey(platform)){
                StreamServiceProperties streamService = streamServiceMapProperties.getRecordUrlService().get(platform);
                Map<String, String> paramMap = new HashMap<>();
                paramMap.put("channelCode", channelCode);
                paramMap.put("stime", stime);
                paramMap.put("etime", etime);
                paramMap.put("scheme", scheme);
                String url =  streamServiceMapProperties.queryStreamService(streamService, paramMap);
                if (StrUtils.isNotNull(url)){
                    return "{\"url\":\"" + url + "\"}";
                }
            }
        }
        return null;
    }

    /**
     * 编辑通道
     * @param channel 通道信息
     * @param userid 操作用户编号
     */
    @Override
    public Long editChannelById(DbFfskyeyeChannel channel, String userid){
        LocalDateTime time = LocalDateTime.now();
        channel.setUpdatedBy(userid);
        channel.setUpdatedTime(time);
        if (channel.getId() != null){
            baseMapper.updateById(channel);
        }else{
            channel.setChannelCode(UUID.randomUUID().toString());

            channel.setCreatedBy(userid);
            channel.setCreatedTime(time);
            channel.setIsDelete(0);
            baseMapper.insert(channel);
        }
        return channel.getId();
    }

    /**
     * 删除通道
     * @param channelId 通道编号
     * @param userid 操作用户编号
     */
    @Override
    public void deleteChannelById(Long channelId, String userid){
        UpdateWrapper<DbFfskyeyeChannel> wrapper = new UpdateWrapper<>();
        wrapper.eq("is_delete",0);
        wrapper.eq("id", channelId);

        wrapper.set("is_delete", 1);
        wrapper.set("updated_by", userid);
        wrapper.set("updated_time", LocalDateTime.now());
        baseMapper.update(null, wrapper);
    }

    /**
     * 根据设备id查询通道集合
     * @param deviceId   设备id
     * @return
     */
    @Override
    public List<DbFfskyeyeChannel> queryChannelListByDeviceId(Long deviceId) {
        QueryWrapper<DbFfskyeyeChannel> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        wrapper.eq("device_id",deviceId);
        return baseMapper.selectList(wrapper);
    }

    /**
     * 修改通道在线状态
     * @param platformCode 平台标识
     * @param platformChannelCode 平台通道唯一标识
     * @param online 通道是否在线(0:离线,1:在线)
     */
    public void changeCameraOnlineStatus(String platformCode, String platformChannelCode, Integer online){
        UpdateWrapper<DbFfskyeyeChannel> wrapper = new UpdateWrapper<DbFfskyeyeChannel>();
        wrapper.eq("is_delete",0);
        wrapper.eq("platform_code", platformCode);
        wrapper.eq("platform_channel_code",platformChannelCode);
        wrapper.set("online", online);
        baseMapper.update(null, wrapper);
    }

    /**
     * 同步通道在线状态
     * @param platformName 设备平台：海康，大华
     * @param userId 用户编号
     */
    @Override
    public void synChannelOnlineStatus(String platformName,String userId) {
        DbFfskyeyePlatform platform = dbFfskyeyePlatformService.queryPlatformByName(platformName);
        String platformCode = platformName;
        if (platform != null) {
            platformCode = platform.getPlatformCode();
        }
        List<DbFfskyeyeChannel> resultList = new ArrayList<>();
        if (PlatformEnum.HIKVERSION.getPlatformCode().equals(platformCode)) {
            List<DbHikCamera> cameraList = dbHikCameraService.queryAllCamera();
            if (null == cameraList || cameraList.size() <= 0) {
                return;
            }
            for (DbHikCamera camera : cameraList) {
                //在线状态（0-未知，1-在线，2-离线）
                Integer online = 0;
                if (camera.getOnlineStatus() != null){
                    if (camera.getOnlineStatus().equals(1)){
                        online = 1;
                    }else if (camera.getOnlineStatus().equals(2)){
                        online = 0;
                    }
                }
                //通道是否在线(0:离线,1:在线)
                changeCameraOnlineStatus(platformCode, camera.getCameraIndexCode(), online);
            }
        } else {
            List<DbHillstoneChannel> channelList = dbHillstoneChannelService.queryAllChannel();
            if (null == channelList || channelList.size() <0) {
                return;
            }
            for (DbHillstoneChannel camera : channelList) {
                //通道是否在线(0:离线,1:在线)
                changeCameraOnlineStatus(platformCode, camera.getChannelCode(), camera.getOnline());
            }
        }
    }

}
